#include "pch.h"
#include "tabs.h"

using namespace std;

JOUI::UITabs::UITabs(UIBase* hParent, INT x, INT y, INT width, INT height, tabs_type type, INT nID, INT dwStyle, INT dwStyleEx, INT textFormat)
{
	InitSubControl(hParent, x, y, width, height, L"form-tabs", NULL, dwStyle, dwStyleEx, nID, textFormat);
	p_data.type = type;
	SetColor(color_text_normal, UIColor(29, 29, 31, 255));
	SetColor(color_text_hover, UIColor(253, 253, 255, 255));
	SetHeaderSize();
}

void JOUI::UITabs::SetTabsType(tabs_type type)
{
	if (p_data.type != type) {
		p_data.type = type;
		
		ReLine();
		RePane();
	}
}

void JOUI::UITabs::SetTabsPoint(tabs_point point)
{
	if (p_data.point != point) {
		p_data.point = point;

		ReLine();
		RePane();
	}
}

void JOUI::UITabs::EnableStretch(BOOL stretch)
{
	p_data.bstre = stretch;
	if(!p_data.bstre) {
		p_data.hsize = { 80,36 };
	}
	ReLine();
	RePane();
}

void JOUI::UITabs::SetHeaderSize(UINT width, UINT height)
{
	p_data.hsize.cx = width;
	p_data.hsize.cy = height;

	ReLine();
	RePane();
}

void JOUI::UITabs::SetHeaderColor(UIColor hover, UIColor line)
{
	if (!hover.empty()) p_data.hclr[1] = hover;
	if (!line.empty()) p_data.hclr[0] = line;
}

INT JOUI::UITabs::AddPane(LPCWSTR title, INT pID)
{
	tabs_panes nps;
	auto pane = new UIPage(this, 0, 0, 1, 1, title, eos_hidden, 0, pID);

	if (pane) {
		nps.view = pane;

		p_data.list.push_back(nps);
		RePane();
		return (INT)p_data.list.size();
	}
	return -1;
}

INT JOUI::UITabs::DeletePane(size_t index, INT pID)
{
	if (index >= 0 && index < p_data.list.size()) {
		if (p_data.list[index].view) {
			delete p_data.list[index].view;
			p_data.list.erase(p_data.list.begin() + index);
			RePane();
			return (INT)p_data.list.size();
		}
	}
	else if (pID != -1) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			if (p_data.list[i].view->GetID() == pID) {
				delete p_data.list[i].view;
				p_data.list.erase(p_data.list.begin() + i);
				RePane();
				return (INT)p_data.list.size();
			}
		}
	}
	return -1;
}

BOOL JOUI::UITabs::SetTitle(size_t index, LPCWSTR title)
{
	if (index >= 0 && index < p_data.list.size()) {
		p_data.list[index].view->SetText(title);
	}
	return FALSE;
}

void JOUI::UITabs::ClearPane()
{
	DeletePane(-1, 0);
}

LPVOID JOUI::UITabs::GetPane(size_t index, INT pID)
{
	if (index >= 0 && index < p_data.list.size()) {
		return p_data.list[index].view;
	}
	else if (pID != -1) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			if (p_data.list[i].view->GetID() == pID) {
				return p_data.list[i].view;
			}
		}
	}
	return 0;
}

BOOL JOUI::UITabs::SetPane(size_t index, INT pID)
{
	if (index >= 0 && index < p_data.list.size()) {
		if (p_data.select >= 0) p_data.list[p_data.select].view->Show(FALSE);
		p_data.select = (INT)index;
		p_data.list[index].view->Show(TRUE);
		ReLine();
		Redraw();
		return TRUE;
	}
	else if (pID != -1) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			if (p_data.list[i].view->GetID() == pID) {
				if (p_data.select >= 0) p_data.list[p_data.select].view->Show(FALSE);
				p_data.select = (INT)i;
				p_data.list[i].view->Show(TRUE);
				ReLine();
				Redraw();
				return TRUE;
			}
		}
	}
	return FALSE;
}


void JOUI::UITabs::ReLine()
{

		if (p_data.point == left) {
			p_data.line.left = p_data.hsize.cx - 4;
			p_data.line.top = p_data.select * p_data.hsize.cy;
			p_data.line.right = p_data.hsize.cx;
			p_data.line.bottom = p_data.line.top + p_data.hsize.cy;
		}
		else if (p_data.point == top) {
			p_data.line.left = p_data.select * p_data.hsize.cx;
			p_data.line.top = p_data.hsize.cy - 4;
			p_data.line.right = p_data.line.left + p_data.hsize.cx;
			p_data.line.bottom = p_data.hsize.cy;
		}
		else if (p_data.point == right) {
			RECT rc;
			GetRect(rc, grt_client);
			p_data.line.left = rc.right - p_data.hsize.cx;
			p_data.line.top = p_data.select * p_data.hsize.cy;
			p_data.line.right = p_data.line.left + 4;
			p_data.line.bottom = p_data.line.top + p_data.hsize.cy;
		}
		else if (p_data.point == bottom) {
			RECT rc;
			GetRect(rc, grt_client);
			p_data.line.left = p_data.select * p_data.hsize.cx;
			p_data.line.top = rc.bottom - p_data.hsize.cy;
			p_data.line.right = p_data.line.left + p_data.hsize.cx;
			p_data.line.bottom = p_data.line.top + 4;
		}
	
}

void JOUI::UITabs::RePane()
{
	if (p_data.list.size() <= 0) return;

	RECT rc;
	GetRect(rc, grt_client);

	if (p_data.type == none) {
		p_data.hsize.cx = 0, p_data.hsize.cy = 0;
		for (size_t i = 0; i < p_data.list.size(); i++) {
			p_data.list[i].view->SetPos(rc.left, rc.top, rc.right, rc.bottom);
		}
		return;
	}

	if (p_data.bstre) {
		if (p_data.point == left || p_data.point == right) {
			p_data.hsize.cy = rc.bottom / p_data.list.size();
		}
		else {
			p_data.hsize.cx = rc.right / p_data.list.size();
		}
	}

	INT x = 0, y = 0, w = 0, h = 0;
	if (p_data.point == left) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			p_data.list[i].rc.left = 0;
			p_data.list[i].rc.top = i * p_data.hsize.cy;
			p_data.list[i].rc.right = p_data.hsize.cx;
			p_data.list[i].rc.bottom = p_data.list[i].rc.top + p_data.hsize.cy;

			x = p_data.hsize.cx;
			w = rc.right - p_data.hsize.cx;
			h = rc.bottom;

			p_data.list[i].view->SetPos(x, y, w, h);
		}
	}
	else if (p_data.point == top) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			p_data.list[i].rc.left = i * p_data.hsize.cx;
			p_data.list[i].rc.top = 0;
			p_data.list[i].rc.right = p_data.list[i].rc.left + p_data.hsize.cx;
			p_data.list[i].rc.bottom = p_data.hsize.cy;

			y = p_data.hsize.cy;
			w = rc.right;
			h = rc.bottom - p_data.hsize.cy;

			p_data.list[i].view->SetPos(x, y, w, h);
		}
	}
	else if (p_data.point == right) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			p_data.list[i].rc.left = rc.right - p_data.hsize.cx;
			p_data.list[i].rc.top = i * p_data.hsize.cy;
			p_data.list[i].rc.right = rc.right;
			p_data.list[i].rc.bottom = p_data.list[i].rc.top + p_data.hsize.cy;

			w = rc.right - p_data.hsize.cx;
			h = rc.bottom;

			p_data.list[i].view->SetPos(x, y, w, h);
		}
	}
	else if (p_data.point == bottom) {
		for (size_t i = 0; i < p_data.list.size(); i++) {
			p_data.list[i].rc.left = i * p_data.hsize.cx;
			p_data.list[i].rc.top = rc.bottom - p_data.hsize.cy;
			p_data.list[i].rc.right = p_data.list[i].rc.left + p_data.hsize.cx;
			p_data.list[i].rc.bottom = rc.bottom;

			w = rc.right;
			h = rc.bottom - p_data.hsize.cy;

			p_data.list[i].view->SetPos(x, y, w, h);
		}
	}

}


LRESULT JOUI::UITabs::OnMsgProc(HWND hWnd, INT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_PAINT) {
		tabs_paint(hWnd);
	}
	else if (uMsg == WM_LBUTTONDOWN) {
		POINT mpt = { LOWORD(lParam),HIWORD(lParam) };
		mpt.x /= p_data.sdpi; mpt.y /= p_data.sdpi;
		for (size_t i = 0; i < p_data.list.size(); i++) {
			if (PtInRect(&p_data.list[i].rc, mpt)) {
				if (p_data.select != i) {
					INT select = p_data.select;
					p_data.list[p_data.select].view->Show(FALSE);
					p_data.list[i].view->Show(TRUE);
					p_data.select = i;

					if (p_data.type == linear) {
						if (p_data.point == left || p_data.point == right) {
							UIAnimation::Start(this, 0, 0, p_data.line.top, i * p_data.hsize.cy, AniEffect::Default, 10, 0, 1, 1);
						}
						else {
							UIAnimation::Start(this, p_data.line.left, i * p_data.hsize.cx, 0, 0, AniEffect::Default, 10, 0, 1, 1);
						}
					}
					else {
						Redraw();
					}
				}
				break;
			}
		}
	}
	else if (uMsg == WM_EX_EASING) {
		auto easing = (JOUI::info_Animation*)lParam;
		if (p_data.point == left || p_data.point == right) {
			p_data.line.top = easing->nCurrentY;
			p_data.line.bottom = p_data.line.top + p_data.hsize.cy;
		}
		else {
			p_data.line.left = easing->nCurrentX;
			p_data.line.right = p_data.line.left + p_data.hsize.cx;
		}

		Redraw();
	}
	else if (uMsg == WM_MOUSEMOVE)
	{
		p_data.hover = -1;
		POINT mpt = { LOWORD(lParam),HIWORD(lParam) };
		mpt.x /= p_data.sdpi; mpt.y /= p_data.sdpi;
		for (size_t i = 0; i < p_data.list.size(); i++) {
			if (PtInRect(&p_data.list[i].rc, mpt)) {
				p_data.hover = i;
				break;
			}
		}
		Redraw();
	}
	else if (uMsg == WM_MOUSELEAVE)
	{
		p_data.hover = -1;
		Redraw();
	}
	else if (uMsg == WM_SIZE)
	{
		ReLine();
		RePane();
	}
	else if (uMsg == WM_DESTROY)
	{
		p_data.list.clear();
	}
	return S_OK;
}

void JOUI::UITabs::tabs_paint(HWND hWnd)
{
	ps_context ps{};

	if (BeginPaint(ps))
	{
		p_data.sdpi = ps.dpi;
		auto br = new UIBrush(p_data.hclr[0]);

		if (p_data.type == linear) {
			for (size_t i = 0; i < p_data.list.size(); i++) {
				if (p_data.hover == i || p_data.select == i) {
					ps.hCanvas->DrawTextByColor(ps.hFont, p_data.list[i].view->GetText(), -1, ps.dwTextFormat, p_data.list[i].rc.left*ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, p_data.hclr[1]);
				}
				else {
					ps.hCanvas->DrawTextByColor(ps.hFont, p_data.list[i].view->GetText(), -1, ps.dwTextFormat, p_data.list[i].rc.left * ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, m_data.Color.crNormal);
				}
			}
			if (p_data.point == left || p_data.point == right) 
				ps.hCanvas->FillRect(br, p_data.line.left * ps.dpi, 0, p_data.line.right * ps.dpi, ps.uHeight);
			else if(p_data.point == top || p_data.point == bottom) 
				ps.hCanvas->FillRect(br, 0, p_data.line.top * ps.dpi, ps.uWidth, p_data.line.bottom * ps.dpi);

			br->SetColor(p_data.hclr[1]);
			ps.hCanvas->FillRect(br, p_data.line.left * ps.dpi, p_data.line.top * ps.dpi, p_data.line.right * ps.dpi, p_data.line.bottom * ps.dpi);
		}
		else if (p_data.type == card) {
			br->SetColor(p_data.hclr[1]);
			for (size_t i = 0; i < p_data.list.size(); i++) {
				if (p_data.select == i) {
					ps.hCanvas->FillRoundRect(br, p_data.list[i].rc.left * ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, 2);
					ps.hCanvas->DrawTextByColor(ps.hFont, p_data.list[i].view->GetText(), -1, ps.dwTextFormat, p_data.list[i].rc.left * ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, m_data.Color.crHover);
				}
				else if (p_data.hover == i) {
					ps.hCanvas->DrawTextByColor(ps.hFont, p_data.list[i].view->GetText(), -1, ps.dwTextFormat, p_data.list[i].rc.left * ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, p_data.hclr[1]);
				}
				else {
					ps.hCanvas->DrawTextByColor(ps.hFont, p_data.list[i].view->GetText(), -1, ps.dwTextFormat, p_data.list[i].rc.left * ps.dpi, p_data.list[i].rc.top * ps.dpi, p_data.list[i].rc.right * ps.dpi, p_data.list[i].rc.bottom * ps.dpi, m_data.Color.crNormal);
				}
			}
			br->SetColor(p_data.hclr[0]);
			if (p_data.point == left)
				ps.hCanvas->DrawLine(br, p_data.line.right * ps.dpi, 0, p_data.line.right * ps.dpi, ps.uHeight, 1);
			else if (p_data.point == top)
				ps.hCanvas->DrawLine(br, 0, p_data.line.bottom * ps.dpi, ps.uWidth, p_data.line.bottom * ps.dpi, 1);
			else if (p_data.point == right)
				ps.hCanvas->DrawLine(br, p_data.line.left * ps.dpi, 0, p_data.line.left * ps.dpi, ps.uHeight, 1);
			else if (p_data.point == bottom)
				ps.hCanvas->DrawLine(br, 0, p_data.line.top * ps.dpi, ps.uWidth, p_data.line.top * ps.dpi, 1);
		}

		delete br;
		EndPaint();
	}
}